﻿using UnityEngine;
using System.Collections;
using UnityEditor;
using System.IO;
using System.Collections.Generic;

namespace UnityEditor
{
    public class OneKeyChangeAnchorType
    {
        static int modifyCount = 0;
        static List<string> updateFilter = new List<string>()
        {
            "ui_item_tip/tip_desc/sp_tip_bg",
        };

        [MenuItem("slg/一键修改anchor类型")]
        static void OnekeyModifyAnchorType()
        {
            string[] searchFolders = {
                "Assets/Resources/prefab",
            };
            string[] guidArr = AssetDatabase.FindAssets("t:Prefab", searchFolders);
            int count = 0;
            foreach (string guid in guidArr)
            {
                string path = AssetDatabase.GUIDToAssetPath(guid);
                GameObject obj = AssetDatabase.LoadAssetAtPath<GameObject>(path);
                // Debug.Log(obj.name, obj);
                ModifyPrefabAnchor(obj.transform);
                EditorUtility.SetDirty(obj);
                EditorUtility.DisplayProgressBar("修改anchor类型", "修改anchor类型中。。。", (float)count++ / guidArr.Length);
            }

            AssetDatabase.SaveAssets();
            AssetDatabase.Refresh();
            EditorUtility.ClearProgressBar();
            Debug.LogFormat("修改完毕，总共修改了{0}个prefab", modifyCount);

        }

        // [MenuItem("slg/Test GetNodePath")]
        // static void TestGetNodePath()
        // {
        //     Transform[] trans = Selection.GetTransforms(SelectionMode.Unfiltered);
        //     for (int i = 0; i < trans.Length; i++)
        //     {
        //         Debug.Log(GetNodePath(trans[i]));

        //     }
        // }

        static void ModifyPrefabAnchor(Transform gTran)
        {
            UIWidget[] widArr = gTran.GetComponentsInChildren<UIWidget>(true);

            bool modified = false;
            for (int i = 0; i < widArr.Length; i++)
            {
                UIWidget wid = widArr[i];

                if (CheckInUpdateFilter(wid.cachedTransform) == true &&
                    wid.updateAnchors != UIRect.AnchorUpdate.OnUpdate &&
                    (wid.leftAnchor.target != null || wid.rightAnchor.target != null ||
                    wid.topAnchor.target != null || wid.bottomAnchor.target != null))
                {
                    wid.updateAnchors = UIRect.AnchorUpdate.OnUpdate;
                    string path = GetNodePath(wid.cachedTransform);
                    Debug.LogFormat(wid.cachedTransform.root, "【修改成功】，{0}", path);
                    modified = true;
                }
                else if (wid.updateAnchors == UIRect.AnchorUpdate.OnUpdate &&
                   (wid.leftAnchor.target != null || wid.rightAnchor.target != null ||
                   wid.topAnchor.target != null || wid.bottomAnchor.target != null))
                {
                    wid.updateAnchors = UIRect.AnchorUpdate.OnEnable;
                    string path = GetNodePath(wid.cachedTransform);
                    Debug.LogFormat(wid.cachedTransform.root, "【修改成功】，{0}", path);
                    modified = true;
                }
            }

            if (modified == true) modifyCount++;
        }

        /// <summary>
        /// 获取某个节点的全路径
        /// </summary>
        /// <param name="tran"></param>
        /// <returns></returns>
        static string GetNodePath(Transform tran)
        {
            string ret = string.Empty;
            if (tran != null)
            {
                ret = tran.name;
                tran = tran.parent;
            }
            else
            {
                return ret;
            }
            while (tran != null)
            {
                ret = tran.name + "/" + ret;
                tran = tran.parent;
            }

            return ret;
        }

        /// <summary>
        /// 检查是否处在update过滤器里，如果在就用update，否则用onEnable
        /// </summary>
        /// <param name="tran"></param>
        /// <returns></returns>
        static bool CheckInUpdateFilter(Transform tran)
        {
            string path = GetNodePath(tran);
            for (int i = 0; i < updateFilter.Count; i++)
            {
                if (path.Contains(updateFilter[i]) == true)
                {
                    return true;
                }
            }

            return false;
        }
    }


}
